package com.example.koki.dao.NewFrontServlet;

import com.example.koki.bean.NewsBean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

public class NewFrontServlet {package com.dao.servlet;

    @WebServlet("/newsfront.html")
    public class NewsFrontServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String p = req.getParameter("page");
            int page = 1;
            if (p != null && p.equals("")) {
                page = Integer.parseInt(p);
            }
            NewsDao newsDao = new NewsDao();
            List<NewsBean> news = newsDao.getList(page);
            // 给用户当前页数 总条数 总页数
            int totalCount,curPage,totalPage,upPage,downPage;
            totalCount = newsDao.count();
            curPage = page;
            if (totalCount%10 == 0) {
                totalPage = totalCount/10;
            }else {
                totalPage = totalCount / 10 + 1;
            }
            if (curPage == 1) {
                upPage = 1;
            } else {
                upPage = curPage - 1;
            }
            if (curPage == totalPage) {
                downPage = totalPage;
            }else {
                downPage = curPage + 1;
            }
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("curPage", curPage);
            req.setAttribute("totalCount", totalCount);
            req.setAttribute("upPage", upPage);
            req.setAttribute("downPage", downPage);
            req.setAttribute("totalCount", totalCount);
            req.setAttribute("curPage", curPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("upPage", upPage);
            // 传递新闻列表
            req.setAttribute("news", news);
            // 加载显示前端页面
            req.getRequestDispatcher("newsFrontList.jsp").forward(req, resp);
        }
}
